package com.jj.dao.impl;

import com.jj.dao.BaseDao;
import com.jj.dao.PageDao;
import com.jj.entity.Department;
import com.jj.entity.Employee;
import com.jj.entity.Page;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PageDaoImpl extends BaseDao implements PageDao {
    @Override
    public Page<Employee> findByPage(String empName, Page<Employee> page) {
        // 初始化SQL语句的StringBuilder和参数列表
        StringBuffer sb = new StringBuffer();
        List<Object> params = new ArrayList<Object>();
        List<Employee> list = new ArrayList<Employee>();

        // 构建基础查询SQL
        sb.append("select e.*,d.dept_name deptName ");
        sb.append(" from employee e,department d ");
        sb.append(" where e.dept_id = d.id ");
        if(empName != null && !"".equals(empName)) {
            sb.append(" and e.emp_name like '%" + empName + "%'");
        }

        // 添加分页查询条件
        sb.append(" ORDER BY e.id DESC ");
        sb.append(" limit " + (page.getPageNum() - 1) * page.getPageSize() + "," + page.getPageSize());

        // 执行查询
        ResultSet rs = executeQuery(sb.toString(), params.toArray());

        // 遍历查询结果，填充学生对象
        if (rs != null) {
            try {
                while (rs.next()) {
                    Employee employee = new Employee();
                    employee.setId(rs.getInt("id"));
                    employee.setEmpName(rs.getString("emp_name"));
                    employee.setDeptId(rs.getInt("dept_id"));
                    employee.setPosition(rs.getString("position"));
                    employee.setOnboarding(rs.getDate("onboarding"));
                    employee.setPhone(rs.getString("phone"));
                    employee.setImgUrl(rs.getString("img_url"));
                    Department department = new Department();
                    employee.setDepartment(department);
                    department.setDeptName(rs.getString("deptName"));
                    list.add(employee);
                }
            } catch (SQLException e) {
                // 将SQL异常包装为运行时异常抛出
                throw new RuntimeException(e);
            } finally {
                closedAll(rs);
            }
        }

        // 设置分页对象的数据和总记录数
        page.setData(list);
        page.setTotal(getTotal(empName));
        return page;
    }

    @Override
    public int getTotal(String empName) {
        // 初始化总数为0
        int total = 0;
        // 初始化参数列表，用于存储SQL查询的参数
        List<Object> params = new ArrayList<Object>();
        // 基础SQL查询语句，用于获取学生总数
        StringBuffer sb = new StringBuffer();
        sb.append("select count(1) count from employee e, department d ");
        sb.append(" where e.dept_id = d.id ");
        // 如果用户名不为空，则在SQL中添加用户名模糊查询条件
        if(empName != null && !"".equals(empName)){
            sb.append(" and e.emp_name like '%" + empName + "%'");
        }
        // 执行SQL查询，返回结果集
        ResultSet rs = executeQuery(sb.toString(), params.toArray());
        // 如果结果集不为空，则尝试获取查询结果
        if(rs!=null){
            try {
                // 遍历结果集
                while (rs.next()) {
                    // 返回查询结果中的总数
                    return rs.getInt("count");
                }
            } catch (SQLException e) {
                // 如果捕获到SQLException，则将其包装为RuntimeException并抛出
                throw new RuntimeException(e);
            }
            finally {
                // 关闭结果集
                closedAll(rs);
            }
        }
        // 如果查询无结果或发生异常，则返回初始化的总数0
        return total;
    }
}
